﻿namespace IBTrader.Modules.Tws
{
    using System;

    public enum OrderType { LMT, MKT, MKTPROT, STP, REL, TRAIL }
    public enum OrderAction { BUY, SELL }
    public interface IIB
    {

        string Host { get; }
        int Port { get; }
        int ClientId { get; }
        string AccountName { get; }

        void Init(string host, string port, string clientId, string accountName = null);
        void Connect();
        void Disconnect();
        bool IsConnected();
        event EventHandler Connected;

        object CreateContract(string symbol, string securityType, string exchange, string currency, string multiplier = null, string primaryExchange = null, DateTime? dt = null);
        object CreateExecutionFilter();
        object CreateOrder();
        object CreateOrder(OrderAction action, OrderType orderType, double lmtPrice, int quantity, string orderRef, TimeSpan orderDuration);

        void cancelHistoricalData(int reqId);
        void reqAccountUpdates(int subscribe, string acctCode);
        void reqHistoricalData(int tickerId, object contract, string endDateTime, string durationString, string barSizeSetting, string whatToShow, int useRTH, int formatDate);
        void reqContractDetails(int reqId, object contract);
        void reqMktData(int tickerId, object contract, string genericTickList, bool snapshot);
        void cancelOrder(int orderId);
        void reqOpenOrders();
        void cancelMktData(int tickerId);
        void cancelMktDepth(int tickerId);
        void reqNewsBulletins(bool allMessages);
        void cancelRealTimeBars(int tickerId);
        void reqRealTimeBars(int tickerId, object contract, int barSize, string whatToShow, bool useRTH);
        void placeOrder(int id, object contract, object order);
        void reqExecutions(int reqId, object filter);
        void reqMktDepth(int tickerId, object contract, int numRows);
        void reqFundamentalData(int reqId, object contract, string reportType);
        void cancelFundamentalData(int reqId);
        void reqCurrentTime();

        event AxTWSLib._DTwsEvents_errMsgEventHandler errMsg;
        event AxTWSLib._DTwsEvents_historicalDataEventHandler historicalData;
        event AxTWSLib._DTwsEvents_historicalDataEventHandler historicalDataEnd;
        event AxTWSLib._DTwsEvents_tickStringEventHandler tickString;
        event AxTWSLib._DTwsEvents_tickGenericEventHandler tickGeneric;
        event AxTWSLib._DTwsEvents_tickPriceEventHandler tickPrice;
        event AxTWSLib._DTwsEvents_tickSizeEventHandler tickSize;
        event EventHandler<ContractDetailsEventArgs> contractDetails;
        event AxTWSLib._DTwsEvents_contractDetailsEndEventHandler contractDetailsEnd;
        event AxTWSLib._DTwsEvents_accountDownloadEndEventHandler accountDownloadEnd;
        event AxTWSLib._DTwsEvents_nextValidIdEventHandler nextValidId;
        event AxTWSLib._DTwsEvents_realtimeBarEventHandler realtimeBar;
        event EventHandler<OpenOrderEventArgs> openOrder;
        event EventHandler openOrderEnd;
        event EventHandler connectionClosed;

        event AxTWSLib._DTwsEvents_updateAccountValueEventHandler updateAccountValue;
        event AxTWSLib._DTwsEvents_updateAccountTimeEventHandler updateAccountTime;
        event EventHandler<UpdatePortfolioEventArgs> updatePortfolio;
        event AxTWSLib._DTwsEvents_updateMktDepthEventHandler updateMktDepth;
        event AxTWSLib._DTwsEvents_updateMktDepthL2EventHandler updateMktDepthL2;
        event AxTWSLib._DTwsEvents_updateNewsBulletinEventHandler updateNewsBulletin;

        event AxTWSLib._DTwsEvents_managedAccountsEventHandler managedAccounts;
        event AxTWSLib._DTwsEvents_execDetailsEndEventHandler execDetailsEnd;
        event EventHandler<ExecDetailsEventArgs> execDetails;
        event AxTWSLib._DTwsEvents_fundamentalDataEventHandler fundamentalData;
        event AxTWSLib._DTwsEvents_orderStatusEventHandler orderStatus;
    }
    public class ContractDetailsEventArgs : EventArgs
    {
        public int RequestId;
        public object ContractDetails;
    }
    public class ExecDetailsEventArgs : EventArgs
    {
        public object contract;
        public object execution;
        public int reqId;
    }
    public class OpenOrderEventArgs : EventArgs
    {
        public int orderId;
        public object contract;
        public object order;
        public object orderState;
    }
    public class UpdatePortfolioEventArgs : EventArgs
    {
        public string accountName;
        public double averageCost;
        public object contract;
        public double marketPrice;
        public double marketValue;
        public int position;
        public double realizedPNL;
        public double unrealizedPNL;
    }
}
